<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="javax.portlet.*"%>

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>

<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.Properties" %>


<portlet:defineObjects />
<%
PortletPreferences prefs = renderRequest.getPreferences(); //get preferences
PortletURL actionURL = renderResponse.createActionURL();
%>
<%-- Checking whethere this is the first time --%>
<%
    String url = prefs.getValue("jiraUrl", "null");
    if(url.equals("null")){ // this is the first time
        actionURL.setParameter("edit", "link");
%>
        <%-- Give a link to edit --%>
        <a href="<%=actionURL.toString()%>">Set your JIRA Login here</a>
<% }else{
      String myJIRALogin =(String)renderRequest.getAttribute("JIRA_LOGIN");
      String myJIRAUrl =(String)renderRequest.getAttribute("JIRA_URL");
%>


    <div class="portlet-msg-info">
        Assigned Issues Description
    </div>

<%
    //Read the JIRA application database configuration values from the Administration.properties file.
    URL myURL=application.getResource("/WEB-INF/Administration.properties");
    InputStream in = myURL.openStream();
    Properties p = new Properties();
    p.load( in );

    String myurl = p.getProperty("url");
    String mydbName = p.getProperty("database");
    String mydriver = p.getProperty("driverClassName");
    String myuserName = p.getProperty("username");
    String mypassword = p.getProperty("password");

    //Establish a connection with the JIRA application database.
    Connection conn = null;
    Class.forName(mydriver).newInstance();
    conn = DriverManager.getConnection(myurl+mydbName,myuserName,mypassword);

    //Take the paths of the Issue Type icons
    String issueTypeIcon1 =    request.getContextPath() +"/images/bug.gif";
    String issueTypeIcon2 =  request.getContextPath() +"/images/newfeature.gif";
    String issueTypeIcon3 =  request.getContextPath() +"/images/task.gif";
    String issueTypeIcon4 =  request.getContextPath() +"/images/improvement.gif";

    //Take the paths of the issue priority icons.
    String issuePriorityIcon1 =    request.getContextPath() +"/images/priority_blocker.gif";
    String issuePriorityIcon2 =  request.getContextPath() +"/images/priority_critical.gif";
    String issuePriorityIcon3 =  request.getContextPath() +"/images/priority_major.gif";
    String issuePriorityIcon4 =  request.getContextPath() +"/images/priority_minor.gif";
    String issuePriorityIcon5 =  request.getContextPath() +"/images/priority_trivial.gif";

    //--------------------------------Display the issue description of Issues Assigned to the user.---------------------------------
    //Arrays to store the retrieved details of the issues.
    String[] issueTypes = new String[100];
    String[] issueKeys = new String[100];
    String[] issueSummerys = new String[100];
    String[] issuePrioritys = new String[100];
    int meme =0;

    //Retrieve the issue details which are assigned to the user and store them in the arrays.
    try {
        Statement s = conn.createStatement ();
        ResultSet resultset = s.executeQuery ("SELECT issuetype, pkey, SUMMARY, PRIORITY FROM jiraissue WHERE ASSIGNEE='" + myJIRALogin + "'AND RESOLUTION IS NULL ORDER BY pkey");

        while (resultset.next() )
        {
            String myIssueType = resultset.getString("issuetype");
            String myPkey = resultset.getString("pkey");
            String mySummery = resultset.getString("SUMMARY");
            String myPriority = resultset.getString("PRIORITY");


            issueTypes[meme] = myIssueType;
            issueKeys[meme] = myPkey;
            issueSummerys[meme] = mySummery;
            issuePrioritys[meme] = myPriority;
            meme = meme+1;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

        %>
    <%-- Display the results --%>
    <div class="results-grid">
        <table class="taglib-search-iterator">
                <tr class="portlet-section-header results-header">
                    <th class="col-1 ">Type</th>
                    <th class="col-2">Key</th>
                    <th class="col-3">Description</th>
                    <th class="col-4">Priority</th>

                </tr>

                <% for(int i = 0; i < meme; i++){%>
                <tr class="portlet-section-body results-row">
                    <td class="col-1" align ="center">
                        <%
                            //Display the correct image of the issue type according the retrieved issue type from the database.
                            String assignedIssueTypeIconName=null;
                            if (issueTypes[i].equals("1"))
                            {
                                assignedIssueTypeIconName=  issueTypeIcon1;
                            }
                            else if (issueTypes[i].equals("2"))
                            {
                                assignedIssueTypeIconName=  issueTypeIcon2;
                            }
                            else if (issueTypes[i].equals("3"))
                            {
                                assignedIssueTypeIconName= issueTypeIcon3;
                            }
                            else if (issueTypes[i].equals("4"))
                            {
                                assignedIssueTypeIconName=  issueTypeIcon4;
                            }
                            out.println("<img src="+ assignedIssueTypeIconName);
                        %>
                    </td>
                    <td class="col-2"><a href="<% out.println(myJIRAUrl+"/browse/"+issueKeys[i]); %>"><% out.println(issueKeys[i]); %></a></td>
                    <td class="col-3"><% out.println(issueSummerys[i]); %></td>
                    <td class="col-4">
                        <%
                            //Display the correct image of the issue priority according the retrieved issue priority from the database.
                            String assignedIssuePriorityIconName=null;
                            if (issuePrioritys[i].equals("1"))
                            {
                                assignedIssuePriorityIconName=  issuePriorityIcon1;
                            }
                            else if (issuePrioritys[i].equals("2"))
                            {
                                assignedIssuePriorityIconName=  issuePriorityIcon2;
                            }
                            else if (issuePrioritys[i].equals("3"))
                            {
                                assignedIssuePriorityIconName= issuePriorityIcon3;
                            }
                            else if (issuePrioritys[i].equals("4"))
                            {
                                assignedIssuePriorityIconName=  issuePriorityIcon4;
                            }
                            else if (issuePrioritys[i].equals("5"))
                            {
                                assignedIssuePriorityIconName=  issuePriorityIcon5;
                            }
                            out.println("<img src="+ assignedIssuePriorityIconName);
                        %>
                    </td>
                </tr>
                <% } %>
        </table>
    </div>

        <br>
        <div class="portlet-msg-info" align="center">
        Reported Issues Description
        </div>

<%
         //--------------------------------Display the issue description of Issues Assigned to the user.---------------------------------
        //Arrays to store the retrieved details of the issues.
        String[] R_issueTypes = new String[100];
        String[] R_issueKeys = new String[100];
        String[] R_issueSummerys = new String[100];
        String[] R_issuePrioritys = new String[100];
        int R_meme =0;

        //Retrieve the issue details which are reported by the user and store them in the arrays.
        try {
            Statement s2 = conn.createStatement ();
            ResultSet resultset2 = s2.executeQuery ("SELECT issuetype, pkey, SUMMARY, PRIORITY FROM jiraissue WHERE REPORTER='" + myJIRALogin + "' AND RESOLUTION IS NULL ORDER BY pkey" );

            while (resultset2.next() )
            {
                String R_myIssueType = resultset2.getString("issuetype");
                String R_myPkey = resultset2.getString("pkey");
                String R_mySummery = resultset2.getString("SUMMARY");
                String R_myPriority = resultset2.getString("PRIORITY");

                R_issueTypes[R_meme] = R_myIssueType;
                R_issueKeys[R_meme] = R_myPkey;
                R_issueSummerys[R_meme] = R_mySummery;
                R_issuePrioritys[R_meme] = R_myPriority;
                R_meme = R_meme+1;
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
 %>
    <%-- Display the results --%>
    <div class="results-grid">
        <table class="taglib-search-iterator" >
                <tr class="portlet-section-header results-header">
                    <th class="col-1 ">Type</th>
                    <th class="col-2">Key</th>
                    <th class="col-3">Description</th>
                    <th class="col-4">Priority</th>
                </tr>

                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <% for(int i = 0; i < R_meme; i++){%>
                <tr class="portlet-section-body results-row">
                    <td>
                        <%
                           //Display the correct image of the issue type according the retrieved issue type from the database.
                           String reportedIssueTypeIconName=null;
                           if (R_issueTypes[i].equals("1"))
                           {
                               reportedIssueTypeIconName=  issueTypeIcon1;
                           }
                           else if (R_issueTypes[i].equals("2"))
                           {
                               reportedIssueTypeIconName=  issueTypeIcon2;
                           }
                           else if (R_issueTypes[i].equals("3"))
                           {
                               reportedIssueTypeIconName= issueTypeIcon3;
                           }
                           else if (R_issueTypes[i].equals("4"))
                           {
                               reportedIssueTypeIconName=  issueTypeIcon4;
                           }
                           out.println("<img src="+ reportedIssueTypeIconName);
                        %>
                    </td>
                    <td class="col-2"><a href="<% out.println("n"); %>"><% out.println(R_issueKeys[i]); %></a></td>
                    <td class="col-3"><% out.println(R_issueSummerys[i]); %></td>
                    <td class="col-4">
                         <%
                             //Display the correct image of the issue priority according the retrieved issue priority from the database.
                            String reportedIssuePriorityIconName=null;
                            if (R_issuePrioritys[i].equals("1"))
                            {
                                reportedIssuePriorityIconName=  issuePriorityIcon1;
                            }
                            else if (R_issuePrioritys[i].equals("2"))
                            {
                                reportedIssuePriorityIconName=  issuePriorityIcon2;
                            }
                            else if (R_issuePrioritys[i].equals("3"))
                            {
                                reportedIssuePriorityIconName= issuePriorityIcon3;
                            }
                            else if (R_issuePrioritys[i].equals("4"))
                            {
                                reportedIssuePriorityIconName=  issuePriorityIcon4;
                            }
                            else if (R_issuePrioritys[i].equals("5"))
                            {
                                reportedIssuePriorityIconName=  issuePriorityIcon5;
                            }
                            out.println("<img src="+ reportedIssuePriorityIconName);
                        %>
                    </td>
                </tr>
                <% } %>
        </table>
    </div>
    <%
    }
    %>


    <%-- Footer to link to JIRA --%>
    <hr style="border-bottom: dotted 1px"/>
    <div class="portlet-section-footer" >
        Go To: <a href="<%=url%>" >JIRA</a>
    </div>